package com.it.stone_manage.service.Impl;

import com.it.stone_manage.dao.StoneTypeDao;
import com.it.stone_manage.model.*;
import com.it.stone_manage.service.TypeService;
import com.it.stone_manage.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
//事务注解
@Transactional
//日志注解
@Slf4j
public class TypeServiceImpl implements TypeService {
    @Autowired
    private StoneTypeDao typeDao;

    private Result result = new Result();
    //    编辑前的数据
    private TypeData oldData;

    //    多选下拉框查询
    @Override
    public Set<Map<String, String>> searchOptions(OptionSearch optionSearch) {
//        各个搜索条件是否为空
        boolean storeHouseNotEmpty = optionSearch.getStorehouseSearch() != null && optionSearch.getStorehouseSearch().size() > 0;
        boolean nameNotEmpty = optionSearch.getNameSearch() != null && optionSearch.getNameSearch().size() > 0;
        boolean standardsNotEmpty = optionSearch.getStandardsSearch() != null && optionSearch.getStandardsSearch().size() > 0;
        boolean glossNotEmpty = optionSearch.getGlossSearch() != null && optionSearch.getGlossSearch().size() > 0;
//        搜出结果
        List<StoneType> searchTypeList = typeDao.searchOptions(optionSearch);

        return loadResult(storeHouseNotEmpty, nameNotEmpty, standardsNotEmpty, glossNotEmpty, searchTypeList);
    }

    //单选下拉框查询
    @Override
    public Set<Map<String, String>> formOptions(SQLData sqlData) {
        boolean storeHouseNotEmpty = sqlData.getStorehouse() != null && !sqlData.getStorehouse().equals("");
        boolean nameNotEmpty = sqlData.getName() != null && !sqlData.getName().equals("");
        boolean standardsNotEmpty = sqlData.getStandards() != null && !sqlData.getStandards().equals("");
        boolean glossNotEmpty = sqlData.getGloss() != null && !sqlData.getGloss().equals("");
        List<StoneType> searchTypeList = typeDao.searchFormOptions(sqlData);

        return loadResult(storeHouseNotEmpty, nameNotEmpty, standardsNotEmpty, glossNotEmpty, searchTypeList);

    }

    //        循环这个结果，根据传入参数判断是搜索的哪个下拉框
    private Set<Map<String, String>> loadResult(boolean storeHouseNotEmpty, boolean nameNotEmpty, boolean standardsNotEmpty, boolean glossNotEmpty, List<StoneType> searchTypeList) {
        Set<Map<String, String>> result = new HashSet<>();
        for (StoneType stoneType : searchTypeList) {
            if (!storeHouseNotEmpty && !nameNotEmpty && !standardsNotEmpty && !glossNotEmpty) {
                Map<String, String> resultMap = new HashMap<>();
                resultMap.put("value", stoneType.getStoneStorehouse());
                result.add(resultMap);
            } else if (!nameNotEmpty && !standardsNotEmpty && !glossNotEmpty) {
                Map<String, String> resultMap = new HashMap<>();
                resultMap.put("value", stoneType.getStoneName());
                result.add(resultMap);
            } else if (!standardsNotEmpty && !glossNotEmpty) {
                Map<String, String> resultMap = new HashMap<>();
                resultMap.put("value", stoneType.getStoneStandards());
                result.add(resultMap);
            } else if (!glossNotEmpty) {
                Map<String, String> resultMap = new HashMap<>();
                resultMap.put("value", stoneType.getStoneGloss());
                result.add(resultMap);
            } else {
                Map<String, String> resultMap = new HashMap<>();
                resultMap.put("value", stoneType.getStoneType());
                result.add(resultMap);
            }
        }
        return result;
    }

    @Override
    public List<TypeData> searchTypeData(SearchData searchData) {
//        结果集合
        List<TypeData> result = new ArrayList<>();

//        获取一个月之前的时间
        Calendar c = Calendar.getInstance();
        c.set(Calendar.MONTH, c.get(Calendar.MONTH) - 1);

//        设置id
        int id = 1;
//        取得各个搜索条件集合
        List<String> storehouseList = searchData.getStorehouseSearch();
        List<String> nameList = searchData.getNameSearch();
        List<String> standardsList = searchData.getStandardsSearch();
        List<String> glossList = searchData.getGlossSearch();
        List<String> typeList = searchData.getTypeSearch();
//        循环仓库名称搜索
        if (storehouseList != null && storehouseList.size() > 0) {

            for (String storehouse : storehouseList) {
                //        搜索条件对象
                SQLData sqlData = new SQLData();
//                默认查询流水记录
                sqlData.setRecordType(searchData.getRecordType() != null ? searchData.getRecordType() : 2);
//                设置默认时间
                if (searchData.getStartDate() == null) {
                    sqlData.setStartDate(c.getTime());
                }
                if (searchData.getEndDate() == null) {
                    sqlData.setEndDate(new Date());
                } else {
                    sqlData.setStartDate(searchData.getStartDate());
                    sqlData.setEndDate(searchData.getEndDate());
                }
                sqlData.setStorehouse(storehouse);
                if (typeDao.searchNum(sqlData) != null) {
                    TypeData storehouseData = new TypeData();
//                设置搜索条件
                    storehouseData.setId(id++);
                    storehouseData.setStorehouse(storehouse);
                    storehouseData.setNumber(typeDao.searchNum(sqlData));
//                设置children list
                    List<TypeData> nameResult = new ArrayList<>();
                    //        循环石材名称搜索
                    if (nameList != null && nameList.size() > 0) {
                        for (String name : nameList) {
                            sqlData.setName(name);
                            if (typeDao.searchNum(sqlData) != null) {
                                TypeData nameData = new TypeData();

                                nameData.setId(id++);
                                nameData.setStorehouse(storehouse);
                                nameData.setName(name);
                                nameData.setNumber(typeDao.searchNum(sqlData));
                                List<TypeData> standardsResult = new ArrayList<>();
//                        循环规格搜索
                                if (standardsList != null && storehouseList.size() > 0) {
                                    for (String standards : standardsList) {
                                        sqlData.setStandards(standards);
                                        if (typeDao.searchNum(sqlData) != null) {
                                            TypeData standardsData = new TypeData();
                                            standardsData.setId(id++);
                                            standardsData.setStorehouse(storehouse);
                                            standardsData.setName(name);
                                            standardsData.setStandards(standards);
                                            standardsData.setNumber(typeDao.searchNum(sqlData));
                                            List<TypeData> glossResult = new ArrayList<>();
                                            if (glossList != null && glossList.size() > 0) {
                                                for (String gloss : glossList) {
                                                    sqlData.setGloss(gloss);
                                                    if (typeDao.searchNum(sqlData) != null) {
                                                        TypeData glossData = new TypeData();
                                                        glossData.setId(id++);
                                                        glossData.setStorehouse(storehouse);
                                                        glossData.setName(name);
                                                        glossData.setStandards(standards);
                                                        glossData.setGloss(gloss);
                                                        glossData.setNumber(typeDao.searchNum(sqlData));
                                                        List<TypeData> typeResult = new ArrayList<>();
                                                        if (typeList != null && typeList.size() > 0) {
                                                            for (String type : typeList) {
                                                                sqlData.setType(type);
                                                                if (typeDao.searchNum(sqlData) != null) {
                                                                    TypeData typeData = new TypeData();
                                                                    typeData.setId(id++);
                                                                    typeData.setStorehouse(storehouse);
                                                                    typeData.setName(name);
                                                                    typeData.setStandards(standards);
                                                                    typeData.setGloss(gloss);
                                                                    typeData.setType(type);
                                                                    typeData.setNumber(typeDao.searchNum(sqlData));
//                                                每条结果加入到上一个的子集合（children）里
                                                                    typeResult.add(typeData);
                                                                }
                                                            }
                                                            glossData.setChildren(typeResult);
                                                        }
                                                        glossResult.add(glossData);
                                                    }
                                                }
                                                standardsData.setChildren(glossResult);
                                            }
                                            standardsResult.add(standardsData);

                                        }
                                    }
                                    nameData.setChildren(standardsResult);
                                }
                                nameResult.add(nameData);

                            }
                        }
                        storehouseData.setChildren(nameResult);
                    }
                    result.add(storehouseData);


                }
            }
        }

        return result;
    }

    @Override
    public Result beforeSave(TypeData typeData) {

        if (typeData != null) {
            oldData = typeData;
            result.setStatus(true);
            result.setMessage("成功");
        } else {
            result.setStatus(false);
            result.setMessage("系统错误，请稍后再试");
        }
        return result;
    }

    @Override
    public Result deleteTypeData(TypeData typeData) {
        try {
            typeDao.deleteType(typeData);
            result.setStatus(true);
            result.setMessage("删除成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setStatus(false);
            result.setMessage("系统错误，请稍后再试！");
        }

        return result;
    }


    @Override
    public Result saveTypeData(TypeData typeData) {
        try {
            if (oldData != null) {
//               编辑时id不为空
                if (typeData.getId() != null) {
                    typeDao.updateType(typeData, oldData);
                    oldData = null;
                    result.setStatus(true);
                    result.setMessage("修改成功!");
                }
            }
            if (typeData.getId() == null) {
                StoneType stoneType = typeDao.searchType(typeData);
                if (stoneType == null) {
                    typeDao.saveType(typeData);
                    result.setStatus(true);
                    result.setMessage("保存成功!");
                } else {
                    result.setStatus(false);
                    result.setMessage("当前信息已存在！");
                }

            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setStatus(false);
            result.setMessage("系统错误，请稍后再试");
        }

        return result;
    }

}
